import { 
  REGISTER_USER_REQUEST, 
  REGISTER_USER_FAILURE, 
  REGISTER_USER_SUCCESS,
  GET_CODE_REQUEST, 
  GET_CODE_FAILURE, 
  GET_CODE_SUCCESS, 
  GET_CODE_OVER_TIME,
  CLEAR_ERROR
} from '../constants';

const initialState = {
  isRegistering: false,
  isRegister: false,
  isGetCoding: false,
  isGetCode: false,
  statusText: null,
  openSnackbar: false,
  error: null
};

function func(state = initialState, action) {
  switch (action.type) {
    case REGISTER_USER_REQUEST:
      return Object.assign({}, state, {
        isRegistering: true,
        isRegister: false,
        isGetCoding: false,
        isGetCode: false,
        statusText: null,
        openSnackbar: false,
        error: null
      });
    case REGISTER_USER_FAILURE:
      return Object.assign({}, state, {
        isRegistering: false,
        isRegister: false,
        isGetCoding: false,
        isGetCode: false,
        statusText: null,
        openSnackbar: false,
        error: action.payload
      });
    case REGISTER_USER_SUCCESS:
      return Object.assign({}, state, {
        isRegistering: false,
        isRegister: true,
        isGetCoding: false,
        isGetCode: false,
        statusText: '注册成功，3秒后自动跳转',
        openSnackbar: true,
        error: null
      });
    case GET_CODE_REQUEST:
      return Object.assign({}, state, {
        isRegistering: false,
        isRegister: false,
        isGetCoding: true,
        isGetCode: false,
        statusText: null,
        openSnackbar: false,
        error: null
      });
    case GET_CODE_FAILURE:
      return Object.assign({}, state, {
        isRegistering: false,
        isRegister: false,
        isGetCoding: false,
        isGetCode: false,
        error: action.payload
      });
    case GET_CODE_SUCCESS:
      return Object.assign({}, state, {
        isRegistering: false,
        isRegister: false,
        isGetCoding: false,
        isGetCode: true,
        statusText: null,
        openSnackbar: false,
        error: null
      });
    case GET_CODE_OVER_TIME:
      return Object.assign({}, state, {
        isRegistering: false,
        isRegister: false,
        isGetCoding: false,
        isGetCode: false,
        statusText: null,
        openSnackbar: false,
        error: null
      });
    case CLEAR_ERROR:
      return Object.assign({}, state, {
        statusText: null,
        openSnackbar: false,
        error: null
      });
    default:
      return state
  }
};

export default func;